package fun.stgoder.test1;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestLock {
    public static int n = 10;

    public static void main(String[] args) throws InterruptedException {
        final Lock lock = new ReentrantLock();

        Thread t1 = new Thread(() -> {
            for (int j = 0; j < 5; j++) {
                try {
                    //lock.lock();
                    n--;
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println(n);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    //lock.unlock();
                }
            }
        });
        t1.start();

        Thread t2 = new Thread(() -> {
            for (int j = 0; j < 5; j++) {
                try {
                    //lock.lock();
                    n--;
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println(n);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    //lock.unlock();
                }
            }
        });
        t2.start();
    }
}
